package com.intelligence.publisher.controller;

import com.alibaba.fastjson2.JSON;
import com.intelligence.entity.pojo.Notice;
import com.intelligence.entity.pojo.NoticeType;
import com.intelligence.entity.pojo.SysUser;
import com.intelligence.entity.result.Result;
import com.intelligence.publisher.service.NoticeService;
import com.intelligence.publisher.service.NoticeTypeService;
import io.swagger.v3.core.util.Json;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : RulerWood
 * @Data : 2024/5/24 - 15:26
 */
@Slf4j
@RestController
@RequestMapping("/system/publisher")
@RequiredArgsConstructor
public class PublisherController {

    private final NoticeTypeService noticeTypeService;
    private final NoticeService noticeService;
    private final RedisTemplate<String,String> redisTemplate;
    private final RabbitTemplate rabbitTemplate;

    /**
     * 获取所有通知发布类型
     * @return
     */
    @GetMapping("/getAllNoticeType")
    public Result<List<NoticeType>> sendMessage() {
        return Result.build(noticeTypeService.list(),200,"success");
    }


    @PostMapping("/sendTheNewNotice")
    public Result sendMessage(@RequestHeader("token") String token,@RequestBody Notice notice) {
        String s = redisTemplate.opsForValue().get("user:login" + token);
        SysUser sysUser = JSON.parseObject(s, SysUser.class);
        log.info("sysUser----sendTheNewNotice------>{}",sysUser);
        notice.setPublisher(sysUser.getId().longValue());
        noticeService.save(notice);
        rabbitTemplate.convertAndSend("Publisher-exchange","Publisher-routingKey",JSON.toJSONString(notice));
        return Result.build(null,200,"success");
    }


}
